<template>
  <div class="lb-system-wechat">
    <top-nav></top-nav>
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        :rules="subFormRules"
        ref="subForm"
        label-width="140px"
        class="config-form"
      >
        <el-form-item label="站点名称" prop="login_title">
          <el-input
            v-model="subForm.login_title"
            maxlength="10"
            show-word-limit
            placeholder="请输入站点名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="站点 Logo" prop="login_logo">
          <lb-cover
            :fileList="subForm.login_logo"
            @selectedFiles="getCover($event, 'login_logo')"
          ></lb-cover>
        </el-form-item>
		
		
        <el-form-item label="左侧图片" prop="login_left">
          <lb-cover
            :fileList="subForm.login_left"
            @selectedFiles="getCover($event, 'login_left')"
          ></lb-cover>
        </el-form-item>		
		
		
        <el-form-item label="备案类型" prop="login_type">
          <el-radio-group v-model="subForm.login_type">
            <el-radio :label="1">ICP备案/许可证号</el-radio>
            <el-radio :label="2">网站联网备案号</el-radio>
          </el-radio-group>
          <lb-tool-tips
            >用于登录页面展示，以百度备案号为例：
            <div class="mt-sm">ICP备案/许可证号：京ICP证030173号</div>
            <div class="mt-sm">网站联网备案号：京公网安备 11000002000001号</div>
          </lb-tool-tips>
        </el-form-item>
        <el-form-item label="备案号" prop="icp">
          <el-input
            v-model="subForm.icp"
            maxlength="50"
            show-word-limit
            placeholder="请输入备案号"
          ></el-input>
        </el-form-item>
        <el-form-item label="版权信息" prop="copyright">
          <el-input
            v-model="subForm.copyright"
            show-word-limit
            placeholder="请输入站点版权信息"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <lb-button type="primary" @click="submitForm">{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      subForm: {
        login_type: '',
        icp: '',
        login_title: '',
        login_logo: [],
		login_left: [],
        copyright: ''
      },
      subFormRules: {
        login_type: { required: true, type: 'number', message: '请选择备案类型', trigger: 'blur' },
        icp: { required: true, validator: this.$reg.isNoEmpty, text: '备案号', reg_type: 2, trigger: 'blur' },
        login_title: { required: true, type: 'string', message: '请输入站点名称', trigger: 'blur' },
        login_logo: { required: true, type: 'array', message: '请选择站点 Logo', trigger: 'blur' },
		login_left: { required: true, type: 'array', message: '请选择站点左侧图片', trigger: 'blur' },
        copyright: { required: true, type: 'string', message: '请输入站点版权信息', trigger: 'blur' }
      }
    }
  },
  created () {
    this.getFormInfo()
  },
  methods: {
    async getFormInfo () {
      let { code, data } = await this.$api.system.configInfo()
      if (code !== 200) return
	  data.login_logo = data.login_logo ? [{ url: data.login_logo }] : []
	  data.login_left = data.login_left ? [{ url: data.login_left }] : []
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
    },
    getCover (img, key) {
      this.subForm[key] = img
    },
    selectedFiles (imgs, key) {
      this.subForm[key].push(...imgs)
    },
    moveFiles (imgs, key) {
      this.subForm[key] = imgs
    },
    submitForm () {
      this.$refs['subForm'].validate(valid => {
        if (valid) {
          let subForm = JSON.parse(JSON.stringify(this.subForm))
		  subForm.login_logo = subForm.login_logo[0].url
		  subForm.login_left = subForm.login_left[0].url
          this.$api.system.configUpdate(subForm).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-system-wechat {
  width: 100%;
  .config-form {
    .el-input {
      width: 300px;
    }
  }
}
</style>
